package com.saver.modules.sys.service;

import com.saver.common.db.JdbcTool;
import com.saver.common.utils.PageUtils;
import com.saver.common.utils.Query;
import com.saver.common.utils.R;
import com.saver.modules.sys.dao.SysMenuDao;
import com.saver.modules.sys.dao.SysRoleMenuDao;
import com.saver.modules.sys.entity.SysMenuEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

@Service
public class SysMenuService {

    @Autowired
    private SysMenuDao sysMenuDao;

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @Autowired
    private JdbcTool jdbcTool;

    public R nav(Set<Long> roleIds,Long currentUser) {
        if (currentUser==1){
            return this.all();
        }else {
            Object[] objects = roleIds.toArray();
            String sql = "select sm.*,(select count(*) FROM sys_menu where parent_id=sm.id) childrenCount from sys_menu sm where sm.id in (select menu_id from sys_role_menu where role_id in(";
            for (int i = 0; i < objects.length; i++) {
                if (i == objects.length - 1) {
                    sql = sql + objects[i] + ")) order by parent_id,ord,id asc";
                } else {
                    sql = sql + objects[i] + ",";
                }
            }
            List<Map<String, Object>> list = jdbcTool.list(sql);
            return R.ok().put("data", list);
        }
    }

    public R all() {
        List<Map<String, Object>> list = sysMenuDao.findAllMenu();
        return R.ok().put("data", list);
    }

    public R queryPage(Map<String, Object> params) {
        Query query = new Query(params);
        String sql = "select id,parent_id,(select title FROM sys_menu where id=sm.parent_id) parentName,name,title,icon,is_system,hide_in_bread,hide_in_menu,show_always,path from sys_menu sm";
        String q = (String) params.get("q");
        if (q != null && !q.equals("")) {
            sql = sql + " where sm.title like '%" + q + "%' or sm.name like '%" + q + "%'";
        }
        query.put("sidx", "parent_id,ord,id");
        query.put("order", "asc");
        PageUtils pageUtils = jdbcTool.queryPage(sql, query);
        return R.ok().put("page", pageUtils);
    }

    public R info(Integer id) {
        Map<String, Object> map = sysMenuDao.findId(id);
        return R.ok().put("data", map);
    }

    @Transactional
    public R deleteMenu(Integer menuId) {
        Optional<SysMenuEntity> byId = sysMenuDao.findById(menuId);
        if (byId.isPresent()) {
            SysMenuEntity sysMenuEntity = byId.get();

            if (sysMenuEntity.getIsSystem()) {
                return R.error("当前为系统菜单,不允许删除");
            } else {
                sysRoleMenuDao.deleteAllByMenuId(new Long(menuId));
                sysMenuDao.deleteById(menuId);
            }
        }

        return R.ok();
    }
}
